﻿@page "/drawings/{ProjectId}"
@inject NavigationManager NavigationManager
@inject IProjectService ProjectService
@inject IMainPlanService MainPlanService
@inject IDrawingService DrawingService
@inject IIssueService IssueService

<PageTitle>图纸列表</PageTitle>
<div class="row position-relative">
    <div class="col">
        <h5>
            <CompUpdateIcon Roles="admin,pm,mgr" Url="@($"project/update/{ProjectId}")" />
            <span>@_projectDto.OdpNumber</span>
            <span class="badge ms-2 bg-success text-white">@_projectDto.Designer</span>
            <a href="projects">
                <span class="badge ms-2 bg-primary text-white">@_projectDto.ProjectType</span>
            </a>
            <a href="projects">
                <CompRiskLevel Risk="@_projectDto.RiskLevel" />
            </a>
            @_projectDto.Name
        </h5>
        <!--todo:table生产计划（根据ProjectId查询）-->
        <div class="row">
            <div class="col">
                <table class="table table-hover table-striped" style="table-layout: fixed; word-break: break-all;">
                    <thead>
                        <tr>
                            <th style="width: 50px; color: dodgerblue">分批</th>
                            <th style="width: 60px; text-align: center; color: dodgerblue">状态</th>
                            <th style="color: purple">计划发图</th>
                            <th style="color: darkgreen">发图时间</th>
                            <th style="color: purple">计划完工</th>
                            <th style="color: darkgreen">入库时间</th>
                            <th style="color: darkgreen">发货时间</th>
                            <th style="color: blue">装箱信息</th>
                            <th style="color: blue">异常状态</th>
                            <th style="color: blue">经验教训</th>
                        </tr>
                    </thead>
                    <tbody>

                        @if (_projectDto.MainPlanDtos.Count == 0)
                        {
                            <tr>
                                <td colspan="7">无计划...</td>
                            </tr>
                        }
                        else
                        {
                            foreach (var plan in _projectDto.MainPlanDtos)
                            {
                                <tr class="@(plan.AllIssueClosed ? "" : "table-danger")">
                                    <td>
                                        <CompBatch Batch="plan.Batch" />
                                    </td>
                                    <td style="text-align: center">
                                        <CompMainPlanStatus Status="@plan.Status"></CompMainPlanStatus>
                                    </td>
                                    <td style="font-weight: bold; @(plan.DrwReleaseTarget < DateTime.Today && plan.DrwReleaseTime == null ? "color:red" : plan.DrwReleaseTarget < DateTime.Today ? "color:green" : "")">
                                        @plan.DrwReleaseTarget.ToString("MM/dd")
                                    </td>
                                    <td>
                                        <AuthorizeView Roles="admin">
                                            <Authorized>
                                                <!--todo:使用弹窗解决这个问题-->
                                                <a href="mainplan/update/drwreleasetime/@plan.Id">
                                                    <span class="oi oi-cog"></span>
                                                </a>
                                            </Authorized>
                                        </AuthorizeView>
                                        @(plan.DrwReleaseTime != null ?
                                            plan.DrwReleaseTime.Value.ToString("MM/dd") : "")
                                    </td>
                                    <td style="font-weight: bold; @(plan.FinishTime < DateTime.Today && plan.Status < MainPlanStatus_e.入库 ? "color:red" : plan.FinishTime < DateTime.Today ? "color:green" : "")">
                                        @plan.FinishTime.ToString("MM/dd")
                                    </td>
                                    <td>
                                        @(plan.WarehousingTime != null ?
                                            plan.WarehousingTime.Value.ToString("MM/dd") : "")
                                    </td>
                                    <td>
                                        @(plan.ShippingTime != null ?
                                            plan.ShippingTime.Value.ToString("MM/dd") : "")
                                    </td>
                                    <td>
                                        @if (plan.HavePackingList)
                                        {
                                            <a href="pallets/mainplan/@plan.Id">装箱信息</a>
                                        }
                                    </td>
                                    <td>
                                        @if (plan.AllIssueClosed)
                                        {
                                            <a href="issue/add/@plan.Id">报告异常</a>
                                        }
                                        else
                                        {
                                            <a href="issues/mainplan/@plan.Id">
                                                @* <span>查看异常</span> *@
                                                <span>@plan.IssueTitles</span>
                                                <img class="ms-1 me-2" style="width: 23px" src="andon_red.gif" />
                                            </a>
                                        }
                                    </td>
                                    <td>
                                        @if (plan.Status == MainPlanStatus_e.发货 && plan.AllIssueClosed)
                                        {
                                            <a href="lessons/mainplan/@plan.Id">结束项目</a>
                                        }
                                        else if (plan.Status == MainPlanStatus_e.结束)
                                        {
                                            <a href="lessons/mainplan/@plan.Id">
                                                @(plan.ClosedTime != null ?
                                                    plan.ClosedTime.Value.ToString("MM/dd") : "") 结束
                                            </a>
                                        }
                                        else
                                        {
                                            <a href="lesson/add/@plan.Id">添加经验</a>
                                        }
                                    </td>
                                </tr>
                            }
                        }
                    </tbody>
                </table>
            </div>
            <div class="col">
            </div>
        </div>
        <div class="row">
            <div class="col-sm-3" style="width: 95px">系统合同:</div>
            <CompFile Roles="admin,pm,mgr,dsr"
                      FileUrl="@_projectDto.ContractUrl"
                      UpLoadUrl="@($"project/uploadfiles/{ProjectId}")" />
        </div>
        <div class="row">
            <div class="col-sm-3" style="width: 95px">物料清单:</div>
            <CompFiles Roles="admin,pm,mgr,dsr"
                       FileUrls="@_projectDto.BomUrl"
                       UpLoadUrl="@($"project/uploadfiles/{ProjectId}")" />
        </div>
        <div class="row">
            <div class="col-sm-3" style="width: 95px">其他附件:</div>
            <CompFiles Roles="admin,pm,mgr,dsr"
                       FileUrls="@_projectDto.AttachmentsUrl"
                       UpLoadUrl="@($"project/uploadfiles/{ProjectId}")" />
        </div>
        <div class="row">
            <div class="col-sm-3" style="width: 95px">最终检验:</div>
            <CompFiles Roles="admin,mgr,qc,insp"
                       FileUrls="@_projectDto.FinalInspectionUrl"
                       UpLoadUrl="@($"project/uploadfiles/{ProjectId}")" />

        </div>
        @if (!string.IsNullOrWhiteSpace(_projectDto.SpecialNotes))
        {
            <div class="row">
                <div class="col-sm-3" style="width: 95px">特殊要求:</div>
                <div class="col-sm-9" style="color: red">
                    <CompMultiLineText Text="@_projectDto.SpecialNotes" />
                </div>
            </div>
        }
    </div>
    <!--显示一个二维码-->
    @* <div class="col position-absolute top-0 end-0 me-5" style="text-align: right; width: 20%">
        <img src=@($"https://api.wrdan.com/qr?data={_projectDto.Id}") data-origin=@($"https://api.wrdan.com/qr?data={_projectDto.Id}") alt="">
    </div> *@
    <CompAddButton Roles="admin,pm,mgr,dsr" Url="@($"drawing/add/{ProjectId}")" />
</div>
<table class="table table-hover " style="table-layout: fixed;word-break:break-all;">
    <thead>
        <tr>
            <th style="width: 50px; color: dodgerblue">分批</th>
            <th style="width: 30%">Item编号</th>
            <th>PDF图纸</th>
            <td>截图预览</td>
        </tr>
    </thead>
    <tbody>
        @if (_projectDto.DrawingDtos.Count == 0)
        {
            <p>没有图纸，请点击右上角添加</p>
        }
        else
        {
            @foreach (var drawing in _projectDto.DrawingDtos)
            {
                <DrawingRow DrawingDto="drawing" />
            }
        }
    </tbody>
</table>

@code {
    [Parameter]
    public string ProjectId { get; set; }
    private ProjectDto _projectDto = new();

    protected override async Task OnInitializedAsync()
    {
        var pResult = await ProjectService.GetFirstOrDefaultAsync(Guid.Parse(ProjectId));
        if (pResult.Status) _projectDto = pResult.Result;

        _projectDto.MainPlanDtos = await MainPlanService.GetAllByProjectIdAsync(Guid.Parse(ProjectId)); ;

        foreach (var mainPlanDto in _projectDto.MainPlanDtos)
        {
            var issueDtos = await IssueService.GetAllByMainPlanIdAsync(mainPlanDto.Id.Value);
            mainPlanDto.AllIssueClosed = true;
            foreach (var issueDto in issueDtos)
            {
                mainPlanDto.AllIssueClosed = mainPlanDto.AllIssueClosed && issueDto.IsClosed;
            }
        }

        var dResult = await DrawingService.GetAllByProjectIdAsync(Guid.Parse(ProjectId));
        if (dResult.Status) _projectDto.DrawingDtos= dResult.Result;


        
    }
}
